package com.yanqu.road.server.cmd.activity.jiuzhou;

import com.yanqu.road.entity.activity.jiuzhou.config.JiuZhouConfig;
import com.yanqu.road.entity.activity.jiuzhou.config.JiuZhouSkillConfig;
import com.yanqu.road.logic.helper.ActivityHelper;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.jiuzhou.CrossJiuZhouProto;
import com.yanqu.road.pb.activity.jiuzhou.JiuZhouProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.PreReduceModule;
import com.yanqu.road.server.manger.activity.jiuzhou.JiuZhouMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.property.Property;

@Cmd(code = GameProtocol.S_JIU_ZHOU_CHOOSE_SKILLID2, desc = "选择棋书")
public class JiuZhouChooseSkillId2Cmd extends AbstractCommand {

    private int activityId;

    private int roomId;

    private long opponentUserId;

    private int skillId2;


    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        JiuZhouProto.JiuZhouChooseSkillId2ReqMsg reqMsg = JiuZhouProto.JiuZhouChooseSkillId2ReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        roomId = reqMsg.getRoomId();
        opponentUserId = reqMsg.getOpponentUserId();
        skillId2 = reqMsg.getSkillId2();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        JiuZhouConfig config = JiuZhouMgr.getConfig();
        if (config == null) {
            ret = GameErrorCode.E_JIU_ZHOU_CONFIG_NOT_EXIST;
            return;
        }
        if (!ActivityHelper.activityInShowTime(config.getActivityInfo())) {
            ret = GameErrorCode.E_JIU_ZHOU_ACTIVITY_NOT_IN_TIME;
            return;
        }
        if (config.getActivityInfo().getActivityId() != activityId) {
            ret = GameErrorCode.E_JIU_ZHOU_ACTIVITY_NOT_IN_TIME;
            return;
        }
        if (skillId2 != 0) {
            // 判断道具是否足够
            JiuZhouSkillConfig skillConfig = config.getSkillConfig(skillId2);
            if (skillConfig == null) {
                ret = GameErrorCode.E_JIU_ZHOU_CONFIG_NOT_EXIST;
                return;
            }
            Property cost = new Property(skillConfig.getItemID(), 1);
            if (!player.getModule(CurrencyModule.class).currencyIsEnough(cost)) {
                ret = GameErrorCode.E_BAG_ITEM_NO_ENOUGH;
                return;
            }
            // 预扣除
            player.getModule(PreReduceModule.class).preReduce(cost);
        }


        // 上个跨服
        CrossJiuZhouProto.CrossJiuZhouChooseSkillId2ReqMsg.Builder builder = CrossJiuZhouProto.CrossJiuZhouChooseSkillId2ReqMsg.newBuilder();
        builder.setActivityId(activityId);
        builder.setRoomId(roomId);
        builder.setOpponentUserId(opponentUserId);
        builder.setSkillId2(skillId2);
        player.sendPacket(Protocol.C_CROSS_JIU_ZHOU_CHOOSE_SKILLID2, builder);
    }

    @Override
    public void sendData(GamePlayer player) {
        if (ret != 0) {
            JiuZhouProto.JiuZhouChooseSkillId2RespMsg.Builder respMsg =  JiuZhouProto.JiuZhouChooseSkillId2RespMsg.newBuilder();
            respMsg.setRet(ret);
            player.sendPacket(ClientProtocol.U_JIU_ZHOU_CHOOSE_SKILLID2, respMsg);
        }
    }
}
